package com.top.ruizhong.mode.vm

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.module.lemlin.base.BaseViewModel
import com.module.lemlin.mode.SubscribeListener
import com.module.lemlin.mode.ViewModelSubscribe
import com.module.lemlin.rxhttp.HttpServiceData
import com.module.lemlin.rxhttp.HttpServiceResponse
import com.top.ruizhong.app.RZApplication
import com.top.ruizhong.entity.UserLoginResponse
import com.top.ruizhong.entity.UserMeResponse
import com.top.ruizhong.entity.UserSmsResponse
import com.top.ruizhong.entity.VideoDraftResponse
import com.top.ruizhong.mode.api.*
import io.reactivex.rxjava3.disposables.Disposable

class UserViewModel(application: Application) : BaseViewModel(application) {


    val userMeLiveData = MutableLiveData<HttpServiceResponse<UserMeResponse>>()
    val videoDraftLiveData = MutableLiveData<HttpServiceResponse<VideoDraftResponse>>()

    /**
     * 获取个人信息
     * */
    fun userMe() {
        val listener = object : ViewModelSubscribe<UserMeResponse>(userMeLiveData) {
            override fun onSuccess(resp: UserMeResponse) {
                super.onSuccess(resp)
                RZApplication.setUserInfoMe(resp.data)
            }
        }
        val disposable: Disposable = userMe(listener)
        dispose.add(disposable)
    }


    val userSmsLiveData = MutableLiveData<HttpServiceResponse<UserSmsResponse>>()

    /**
     * 发送短信验证码
     *
     * @param mobile 手机号
     * @param event 事件：login,set_pwd
     * */
    fun userSmsSend(mobile: String, event: String) {
        val listener = object : ViewModelSubscribe<UserSmsResponse>(userSmsLiveData) {
            override fun onSuccess(resp: UserSmsResponse) {
                super.onSuccess(resp.apply { data = mobile })
            }
        }
        val disposable: Disposable =
            com.top.ruizhong.mode.api.userSmsSend(mobile, event, listener)
        dispose.add(disposable)
    }


    val userSetPwdLiveData = MutableLiveData<HttpServiceResponse<HttpServiceData>>()

    /**
     * 设置访问密码
     *
     * @param smsCode 短信验证码
     * @param password 密码
     * @param confirmPassword 确认密码
     * */
    fun userSetPwd(
        smsCode: String, password: String, confirmPassword: String
    ) {
        val disposable: Disposable =
            userSetPwd(smsCode, password, confirmPassword, ViewModelSubscribe(userSetPwdLiveData))
        dispose.add(disposable)
    }


    var page: Int = 1
    var limit: Int = 20

    /**
     * 我的作品 / 草稿箱
     *
     * @param status 状态 0草稿箱,1待审核,2审核通过,3:审核拒绝
     * */
    fun userMyVideos(status: Int, isLoadMore: Boolean = false) {
        val listener = object : ViewModelSubscribe<VideoDraftResponse>(videoDraftLiveData) {
            override fun onSuccess(resp: VideoDraftResponse) {
                super.onSuccess(resp.apply { loadMore = page > 1 })
                page += 1
            }
        }
        if (!isLoadMore)
            page = 1
        val disposable: Disposable =
            userMyVideos(status.toString(), page.toString(), limit.toString(), listener)
        dispose.add(disposable)
    }

    /**
     * 私人草稿箱视频
     *
     * @param password 访问密码
     * */
    fun userPrivateDraftVideos(password: String, isLoadMore: Boolean = false) {
        val listener = object : ViewModelSubscribe<VideoDraftResponse>(videoDraftLiveData) {
            override fun onSuccess(resp: VideoDraftResponse) {
                super.onSuccess(resp.apply { loadMore = page > 1 })
                page += 1
            }
        }
        if (!isLoadMore)
            page = 1
        val disposable: Disposable =
            userPrivateDraftVideos(password, page.toString(), limit.toString(), listener)
        dispose.add(disposable)
    }


    val passwordLiveData = MutableLiveData<String>()

    /**
     * 临时保存用户密码
     * */
    fun setPasswordSaveInterim(pwd: String?) {
        passwordLiveData.postValue(pwd)
    }


    val videoDeleteLiveData = MutableLiveData<HttpServiceResponse<HttpServiceData>>()
    /**
     * 删除视频
     *
     * @param videoIds 视频id,多个用逗号隔开
     * */
    fun videoDelete(videoIds: String) {
        val disposable: Disposable = videoDelete(
            videoIds, ViewModelSubscribe(videoDeleteLiveData)
        )
        dispose.add(disposable)
    }
}